package com.example.demo;

import com.alibaba.druid.pool.DruidDataSource;
import org.quartz.*;
import org.springframework.boot.autoconfigure.quartz.QuartzDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class SchedulerConfiguration {

    @Bean
    @QuartzDataSource
    public DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        // 以下整合数据可定义在配置文件中
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        dataSource.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
        dataSource.setUsername("Saka");
        dataSource.setPassword("123456");
        dataSource.setInitialSize(5);
        dataSource.setMaxActive(20);
        dataSource.setMinIdle(5);
        dataSource.setMaxWait(30000);
        dataSource.setTestOnBorrow(false);
        dataSource.setTestOnReturn(false);
        dataSource.setTestWhileIdle(true);
        dataSource.setDefaultAutoCommit(true);
        return dataSource;
    }

    @Bean(name="jobDetail")
    public JobDetail getSimpleJobDetail(){
        return JobBuilder.newJob(SimpleJob.class).withIdentity("SimpleJob").storeDurably().build();
    }

    @Bean
    public Trigger getTrigger(JobDetail jobDetail){
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(2).repeatForever();

        return TriggerBuilder.newTrigger().forJob(jobDetail).withIdentity("SimpleTrigger").withSchedule(scheduleBuilder).build();

    }

    @Bean(name="simpleJob")
    public JobDetail getSimpleJobDetail2(){
        return JobBuilder.newJob(SimpleJob2.class).withIdentity("SimpleJob2").storeDurably().build();
    }

    @Bean
    public Trigger getTrigger2(JobDetail simpleJob){

        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/3 * * * * ?");

        return TriggerBuilder.newTrigger().forJob(simpleJob).withIdentity("SimpleTrigger2").withSchedule(cronScheduleBuilder).build();

    }

}